package com.bbf.tio.server;

import com.bbf.tio.common.TioAbstractHandler;
import com.bbf.tio.common.TioConstants;
import com.bbf.tio.common.TioPacket;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.server.intf.ServerAioHandler;

public class TioServerAioHandler extends TioAbstractHandler
    implements ServerAioHandler<Object, TioPacket, Object> {
  /**
   * 处理消息
   */
  @Override
  public Object handler(TioPacket packet, ChannelContext<Object, TioPacket, Object> channelContext) throws Exception {
    byte[] body = packet.getBody();
    if (body != null) {
      String str = new String(body, TioConstants.CHARSET_UTF8);
      System.out.println("收到消息：--" + str + "--");
      TioPacket resppacket = new TioPacket();
      resppacket.setBody(("--服务器回复：接受到的消息是--" + str + "--").getBytes(TioConstants.CHARSET_UTF8));
      Aio.send(channelContext, resppacket);
    }
    return null;
  }
}
